package com.manger.format;

import com.manger.common.office.sax.ExcelFactory;
import com.manger.model.ManBang;
import com.manger.model.Project;
import com.manger.utils.CopyFiles;
import com.manger.utils.LoanFileEnum;
import com.manger.utils.SystemConstant;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName ManBangCaseFilesFormat
 * @Description TODO
 * @Author @LiuHongJun
 * @Date 2025/9/13 17:13
 * @Version 1.0
 */
public class ManBangCaseFilesFormat implements CaseFilesFormat{

    @Override
    public void format(String extractDir, String targetDir, Project project) throws Exception {
        String excelFilePath = extractDir + "/" + SystemConstant.ENTRUSTED_INFO;
        File file = new File(excelFilePath);
        if(!file.exists()){
            System.out.println("没有检查到委托信息表!");
            return;
        }
        Map<String, List<?>> map = ExcelFactory.getExcelFactory().getParseExecutor().parse(new File(excelFilePath), ManBang.class).get();
        if(map.keySet().size() <= 0){
            System.out.println("没有解析到委托信息表数据！");
            return;
        }
        List<ManBang> manBangList = (List<ManBang>) map.get(map.keySet().iterator().next());
        if(manBangList == null){
            System.out.println("没有解析到委托信息表数据！");
            return;
        }
        String repayPath = extractDir + "/" + project.getDebtName() + "/" + "还款明细";
        String repayPdfPath = extractDir + "/" + project.getDebtName() + "/" + "回单";
        String loanContractPath = extractDir + "/" + project.getDebtName() + "/" + "借款合同";

        //还款明细
        Map<String,String> repayMap = new HashMap<>();
        File repayFile = new File(repayPath);
        File[] repayFiles = repayFile.listFiles();
        for(File temp: repayFiles){
            String fileName = temp.getName();
            String idCard = getFileNumberCode(fileName,"");
            repayMap.put(idCard,temp.getCanonicalPath());
        }

        //回单
        Map<String,String> repayPdfMap = new HashMap<>();
        File repayPdfFile = new File(repayPdfPath);
        File[] repayPdfFiles = repayPdfFile.listFiles();
        for(File temp: repayPdfFiles){
            String fileName = temp.getName();
            String contractCode = getFileNumberCode(fileName,"");
            repayPdfMap.put(contractCode,temp.getCanonicalPath());
        }

        //借款合同
        Map<String,String> loanContractMap = new HashMap<>();
        File loanContractFile = new File(loanContractPath);
        File[] loanContractFiles = loanContractFile.listFiles();
        for(File temp: loanContractFiles){
            String fileName = temp.getName();
            String contractCode = getFileNumberCode(fileName,"");
            loanContractMap.put(contractCode,temp.getCanonicalPath());
        }

        Map<String,String> resultMap = new HashMap<>();
        for(ManBang manBang: manBangList){
            if(StringUtils.isEmpty(manBang.getLoanUserName())){
                continue;
            }
            String tempTargetPath = targetDir + "/" + project.getDebtName() + "/" + SystemConstant.CASE_FILE_DIR_NAME + "/"
                    + manBang.getLoanUserName() + "-" + manBang.getIdCard();
            File tempFile = new File(tempTargetPath);
            if(!tempFile.exists()){
                tempFile.mkdirs();
                CopyFiles.createDirs(tempTargetPath);
            }
            resultMap.put(manBang.getIdCard(),tempTargetPath);
            String tempRepayPath = repayMap.get(manBang.getIdCard());
            if(StringUtils.isNotEmpty(tempRepayPath)){
                CopyFiles.copyFileToDir(tempRepayPath,tempTargetPath + "/" + LoanFileEnum.EVIDENCE_MATERIALS.getName());
            }

            String tempRepayPdfPath = repayPdfMap.get(manBang.getLoanCode());
            if(StringUtils.isNotEmpty(tempRepayPdfPath)){
                CopyFiles.copyFileToDir(tempRepayPdfPath,tempTargetPath + "/" + LoanFileEnum.EVIDENCE_MATERIALS.getName());
            }
            String tempLoanContractPath = loanContractMap.get(manBang.getLoanCode());
            if(StringUtils.isNotEmpty(tempLoanContractPath)){
                CopyFiles.copyFileToDir(tempLoanContractPath,tempTargetPath + "/" + LoanFileEnum.EVIDENCE_MATERIALS.getName());
            }
            System.out.println("整理数据：【"+manBang.getLoanUserName() + "-" + manBang.getIdCard() +"】" );
        }
        //拷贝诉讼状
        File tempFile = new File(extractDir + "/" + project.getDebtName() + "/");
        if(tempFile.exists()){
            System.out.println("整理其他材料");
           File[] tempFiles = tempFile.listFiles();
           for(File temp: tempFiles){
               String dirName = temp.getName();
               File[] files = temp.listFiles();
               for(File e: files){
                    for(String key: resultMap.keySet()){
                        if(e.getName().contains(key)){
                            CopyFiles.copyFileToDir(e.getAbsolutePath() , resultMap.get(key) + "/" + dirName);
                        }
                    }
               }
           }
        }
        System.out.println("拷贝通用材料");
        //拷贝通用材料
        for(String key: resultMap.keySet()){
            CopyFiles.copyBaseFiles(resultMap.get(key),project);
        }
    }

    public String getFileNumberCode(String fileName,String delimiter) {
        int index = fileName.indexOf(delimiter); // 查找指定字符的位置
        if (index != -1) {
            String afterDelimiter = fileName.substring(index + delimiter.length()); // 获取指定字符后的部分字符串
            Pattern pattern = Pattern.compile("\\d+"); // 匹配一个或多个数字
            Matcher matcher = pattern.matcher(afterDelimiter);
            if (matcher.find()) {
                return matcher.group();
            }
        }
        return null;
    }

    public static void main(String[] args) throws Exception{
        ManBangCaseFilesFormat manBangCaseFilesFormat = new ManBangCaseFilesFormat();
        Project project = new Project();
        project.setDebtName("满帮");
        String extractDir = "C:\\Users\\liuhj\\Desktop\\满帮例\\例\\满帮";
        String targetDir = "C:\\Users\\liuhj\\Desktop\\案件整理结果";
        manBangCaseFilesFormat.format(extractDir,targetDir,project);
    }
}
